﻿namespace Innovative.ProXr
{
	/// <summary>
	/// Converts an API that responds with 0 or 1 to indicate the status
	/// of a relay to an enumeration.
	/// </summary>
	public class RelayStatusResponseConverter : IResponseConverter<RelayStatus>
	{
		RelayStatus IResponseConverter<RelayStatus>.Convert(RelayByte[] response)
		{
			RelayStatus returnValue = RelayStatus.Unknown;

			if (response.Length == 1)
			{
				if (response[0] == 1)
				{
					returnValue = RelayStatus.On;
				}
				else if (response[0] == 0)
				{
					returnValue = RelayStatus.Off;
				}
				else
				{
					returnValue = RelayStatus.Unknown;
				}
			}
			else
			{
				throw new InvalidResponseException("The RelayStatusResponseConverter converter expects a single byte response.");
			}

			return returnValue;
		}
	}
}
